﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using CLEX.AdminUI.View;

namespace CLEX.AdminUI.ViewModel
{
    internal sealed class FriendshipListVM : ViewModelBase
    {
        private readonly Facade facade;
        private readonly ObservableCollection<FriendshipReadVM> friendships = new ObservableCollection<FriendshipReadVM>();
        private FriendshipReadVM selectedFriendship;
        private RelayCommand newFriendship;
        private RelayCommand editFriendship;
        private RelayCommand deleteFriendship;

        public IEnumerable<FriendshipReadVM> Friendships
        {
            get { return friendships; }
        }

        public FriendshipReadVM SelectedFriendship
        {
            get { return selectedFriendship; }
            set
            {
                if (selectedFriendship != value)
                {
                    selectedFriendship = value;
                    FirePropertyChanged("SelectedFriendship");
                }
            }
        }

        public ICommand NewFriendship
        {
            get
            {
                if (newFriendship == null)
                {
                    newFriendship = new RelayCommand(DoNewFriendship);
                }
                return newFriendship;
            }
        }

        public ICommand EditFriendship
        {
            get
            {
                if (editFriendship == null)
                {
                    editFriendship = new RelayCommand(DoEditFriendship, CanEditFriendship);
                }
                return editFriendship;
            }
        }

        public ICommand DeleteFriendship
        {
            get
            {
                if (deleteFriendship == null)
                {
                    deleteFriendship = new RelayCommand(DoDeleteFriendship, CanDeleteFriendship);
                }
                return deleteFriendship;
            }
        }

        public FriendshipListVM(Facade facade)
        {
            this.facade = facade;
            Refresh();
        }

        private void Refresh()
        {
            Dictionary<int, string> users = facade.GetAllUsers().ToDictionary(p => p.UserId, p => p.Username);
            friendships.Clear();
            friendships.AddRange(facade.GetAllFriendships().Select(p => new FriendshipReadVM(p, users[p.User1Id], users[p.User2Id])));
        }

        private void DoNewFriendship()
        {
            FriendshipCreateView vw = new FriendshipCreateView(new FriendshipCreateVM(facade));
            vw.ShowDialog();
            Refresh();
        }

        private void DoEditFriendship()
        {
            FriendshipEditView vw = new FriendshipEditView(new FriendshipEditVM(facade, selectedFriendship.GetData()));
            vw.ShowDialog();
            Refresh();
        }

        private bool CanEditFriendship()
        {
            return SelectedFriendship != null;
        }

        private void DoDeleteFriendship()
        {
            if (facade.ConfirmDelete("frienship"))
            {
                facade.DeleteFriendship(SelectedFriendship.GetData());
                Refresh();
            }
        }

        private bool CanDeleteFriendship()
        {
            return SelectedFriendship != null;
        }
    }
}
